﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityFrameworkDemo.Models
{

    public class RecordModel
        : IEquatable<RecordModel>
    {

        [Key]
#if USE_GUID
        public Guid Id { get; set; }
#else
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)] // Only available for SQL Server, not SQL Server Compact.
        public int Id { get; set; }
#endif

        public string Name { get; set; }

        public DateTime Date { get; set; }

        public bool IsAttend { get; set; }

        public RecordModel()
        {
#if USE_GUID
            this.Id = Guid.NewGuid();
#endif
        }

        public override string ToString()
        {
            return this.ToString(false);
        }

        public string ToString(bool withPropertyName)
        {
            var type = this.GetType();
            var ps = type.GetProperties();
            var sb = new StringBuilder();
            var fmt = withPropertyName ? "{0}: {1}\t" : "{1}\t";
            foreach (var pi in ps)
            {
                sb.AppendFormat(fmt, pi.Name, pi.GetValue(this));
            }
            return sb.ToString().Trim();
        }

        public override bool Equals(object obj)
        {
            if (obj is RecordModel)
            {
                return this.Equals((RecordModel)obj);
            }
            return base.Equals(obj);
        }

        public bool Equals(RecordModel other)
        {
            return this.Id == other.Id &&
                string.Equals(this.Name, other.Name) &&
                this.Date == other.Date &&
                this.IsAttend == other.IsAttend;
        }

        public override int GetHashCode()
        {
            return this.Id.GetHashCode() ^ this.Name.GetHashCode() ^ this.Date.GetHashCode() ^ this.IsAttend.GetHashCode();
        }

    }

}